package com.zzyl.nursing.controller;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Device;
import com.zzyl.nursing.dto.DeviceDto;
import com.zzyl.nursing.service.IDeviceService;
import com.zzyl.nursing.vo.DeviceDetailVo;
import com.zzyl.nursing.vo.ProductVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 设备Controller
 *
 * @author alexis
 * @date 2025-03-05
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/nursing/device")
public class DeviceController extends BaseController {
    @Autowired
    private IDeviceService deviceService;

    /**
     * 查询设备列表
     */
    @ApiOperation("查询设备列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    @GetMapping("/list")
    public TableDataInfo<List<Device>> list(@ApiParam("设备查询条件") Device device) {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }

    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:list')")
    public R<List<ProductVo>> allProduct() {
        return R.ok(deviceService.allProduct());
    }

    /**
     * 从物联网平台同步产品列表
     */
    @ApiOperation("从物联网平台同步产品列表")
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @PostMapping("/syncProductList")
    public R syncProductList() {
        return R.ok(deviceService.syncProductList());
    }

    /**
     * 注册设备
     */
    @ApiOperation("注册设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:add')")
    @PostMapping("/register")
    public R register(@RequestBody DeviceDto deviceDto) {
        deviceService.register(deviceDto);
        return R.ok();
    }

    /**
     * 获取设备详细信息
     */
    @GetMapping("/{iotId}")
    @ApiOperation("获取设备详细信息")
    public R<DeviceDetailVo> deviceDetail(@ApiParam("iotId") @PathVariable("iotId") String iotId) {
        return R.ok(deviceService.queryDeviceDetail(iotId));
    }

    /**
     * 查询设备上报数据
     */
    @GetMapping("/queryServiceProperties/{iotId}")
    @ApiOperation("查询设备上报数据")
    public R<List<Map<String, Object>>> queryServiceProperties(@PathVariable("iotId") String iotId) {
        return R.ok(deviceService.queryServiceProperties(iotId));
    }
    /**
     * 修改设备
     */
    @ApiOperation("修改设备")
    @PreAuthorize("@ss.hasPermi('nursing:device:edit')")
    @PutMapping
    public R edit(@RequestBody DeviceDto deviceDto) {
        deviceService.edit(deviceDto);
        return R.ok();
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/{iotId}")
    @ApiOperation("删除设备")
    public R deleteDevice(@PathVariable("iotId") String iotId) {
        deviceService.deleteDevice(iotId);
        return R.ok();
    }
}
